/*
 * @Author: fhw
 * @Date: 2022-07-31 12:34:59
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-02-04 21:52:47
 * @Description:
 */
import { Body, Controller, Get, HttpCode, Post, Query, UseGuards } from '@nestjs/common'
import { AuthGuard } from '@nestjs/passport'
import { ApiResponse, ApiOperation, ApiTags, ApiBody, ApiQuery } from '@nestjs/swagger'
import { ChatUserService } from 'src/chat_user/chat_user.service'
import { ArticleService } from '../article/article.service'
import { ArticleCommentService } from './article_comment.service'
import { CommentDto, CommentInfoIdDto, CommentPageDto } from './dto/article_comment.dto'
import { CommentResult } from './entities/article_comment.entity'

@Controller('articleComment')
@ApiTags('文章评论')
export class ArticleCommentController {
  constructor(
    private readonly articleCommentService: ArticleCommentService,
    private readonly articleService: ArticleService,
    // 聊天用户
    private readonly chatUserService: ChatUserService,
  ) { }
  /** 分类 */
  @HttpCode(200)
  @Get('/getComment')
  @ApiResponse({
    status: 200,
    description: '获取评论',
    type: CommentResult,
  })
  @ApiOperation({ summary: '获取文章评论', operationId: 'getComment' })
  @UseGuards(AuthGuard('jwt'))//jwt策略，身份鉴权
  async getComment(@Query() params: CommentPageDto) {
    await this.articleService.getArticleInfo({ id: params.articleId })
    return this.articleCommentService.getComment(params)
  }

  @HttpCode(200)
  @Post('/comment')
  @ApiQuery({
    description: '发表评论',
    type: CommentDto,
  })
  @ApiBody({
    description: '发表评论',
    type: CommentDto,
  })
  @ApiOperation({ summary: '发表评论', operationId: 'comment' })
  @UseGuards(AuthGuard('jwt'))//jwt策略，身份鉴权
  async comment(@Body() body: CommentDto) {
    console.log(body.articleId);
    
    await this.articleService.getArticleInfo({ id: body.articleId })
    const user = await this.chatUserService.getUserInfo({ id: body.userId })
    return this.articleCommentService.comment(body, { user })
  }
  @HttpCode(200)
  @Post('/updateCommentLikes')
  @ApiQuery({
    description: '点赞评论',
    type: CommentInfoIdDto,
  })
  @ApiBody({
    description: '点赞评论',
    type: CommentInfoIdDto,
  })
  @ApiOperation({ summary: '点赞评论', operationId: 'updateCommentLikes' })
  @UseGuards(AuthGuard('jwt'))//jwt策略，身份鉴权
  async updateCommentLikes(@Body() body: CommentInfoIdDto) {
    return this.articleCommentService.updateCommentLikes(body)
  }
}
